/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

using EnvDTE;
using EnvDTE80;

using VSLangProj;
using VsWebSite;

using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;

namespace DslEditorPowerToy.Recipes.Actions
{
    public class RemoveAssembliesReferenceAction : ActionBase
    {
        private string _fileNames = string.Empty;
        private Project _referringProject = null;

        #region Constructors
        public RemoveAssembliesReferenceAction() : base()
        {
        }
        #endregion

        #region Public Properties
        [Input(Required = true)]
        public string FileNames
        {
            get
            {
                return this._fileNames;
            }
            set
            {
                this._fileNames = value;
            }
        }
        [Input(Required = true)]
        [CLSCompliant(false)]
        public Project ReferringProject
        {
            get
            {
                return this._referringProject;
            }
            set
            {
                this._referringProject = value;
            }
        }
        #endregion

        #region IAction Members

        public override void Execute()
        {
            base.Execute();
            ValidateParameters();

            //Get list of names
            List<string> list = this.BuildAssembliesNameList();
            
            //Check if web project
            if (true == DteHelper.IsWebProject(this._referringProject))
            {
                VSWebSite site = this._referringProject.Object as VSWebSite;
                if (site != null)
                {
                    //Remove References
                    foreach (string assemblyName in list)
                    {
                        foreach (AssemblyReference reference in site.References)
                        {
                            if (0 == string.Compare(reference.Name, assemblyName, true))
                            {
                                reference.Remove();
                                break;
                            }
                        }
                    }
                }
                return;
            }

            //Regular project
            VSProject project = this._referringProject.Object as VSProject;
            if (null != project)
            {
                //Remove references
                foreach (string assemblyName in list)
                {
                    Reference reference = project.References.Find(assemblyName);
                    if (null != reference)
                        reference.Remove();
                }
            }
        }

        public override void Undo()
        {
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Validates parameters before execution
        /// </summary>
        protected override void ValidateParameters()
        {
            if (null == this._fileNames)
                throw new ArgumentNullException("FileNames");
            if (0 == this._fileNames.Length)
                throw new ArgumentOutOfRangeException("FileNames");
            if (null == this._referringProject)
                throw new ArgumentNullException("ReferringProject");
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Build list of assembly names
        /// </summary>
        /// <returns></returns>
        private List<string> BuildAssembliesNameList()
        {
            List<string> list = new List<string>();
            list.AddRange(this._fileNames.Split(new char[] { ';' }));
            return list;
        }
        #endregion
    }
}
 
